package top.shen33.yang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import top.shen33.yang.base.R;
import top.shen33.yang.model.entity.SysUser;
import top.shen33.yang.services.SysUserService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author shen
 * @since 2018-07-16
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController<SysUser> {

    @Autowired
    private SysUserService sysUserService;


    @GetMapping(value = "test")
    public void test(){
//        //测试逻辑删除功能
        SysUser sysUser = sysUserService.getById(1);
//        System.out.println(sysUser.toString());
//        //测试分页
//        IPage<SysUser> sysUserIPage = sysUserService.selectPage(
//                new Page<SysUser>(1, 10), new QueryWrapper<>());

        //测试公共字段自动填充
        SysUser sysUser1 = new SysUser();
//        sysUser1.setUsername("shen");
//        sysUser1.setNickname("shen");
//        sysUser1.setPassword("shen");
        System.out.println(sysUserService.save(sysUser1));
        System.out.println(sysUserService.saveOrUpdate(sysUser1));

        SysUser sysUser2 = new SysUser();
//        sysUser2.setUsername("shen2");
//        sysUser2.setNickname("shen2");
//        sysUser2.setPassword("shen2");


        sysUserService.saveOrUpdate(sysUser2);

        throw  new RuntimeException("hah");

    }


    @GetMapping(value = "test1/{id}")
    public Object test1(@PathVariable Integer id){
        SysUser sysUser = sysUserService.selectTestById(id);
        return sysUser;

    }
    @RequestMapping(value = "/all")
    public Object all(SysUser sysUser){
        List<SysUser> userList = sysUserService.list(new QueryWrapper<>());
        return userList;

    }

    @Override
    public void exportExcel(HttpServletResponse response, SysUser sysUser) throws Exception {

    }

    @Override
    public Object importExcel(MultipartFile file) throws Exception {
        return null;
    }

    @Override
    public Object doAdd(@RequestBody SysUser sysUser) throws Exception {

        sysUserService.doAdd(sysUser);
        return R.ok(sysUser);
    }

    @Override
    public Object deDelete(String[] ids) throws Exception {

        sysUserService.doDelete(ids);
        return R.ok("delete ok");
    }

    @Override
    public Object doUpdate(@RequestBody SysUser sysUser) throws Exception {

        sysUserService.doUpdate(sysUser);
        return R.ok(sysUser);
    }

    @Override
    public Object getOne(@PathVariable String id) throws Exception {
        SysUser sysUser = sysUserService.getOne(id);
        return R.ok(sysUser);
    }

    @Override
    public Object pageInfo(Page page, SysUser sysUser) throws Exception {

        Page<SysUser> sysUserPage = sysUserService.pageInfo(page, sysUser);
        return R.ok("page",sysUserPage);
    }


    @Override
    public Object home() {
        throw new RuntimeException("呵呵， 测试用的");
    }
}

